package com.example.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.BaseOnclickListener;
import com.example.myapplication.R;
import com.example.myapplication.entity.Hots;

import java.util.List;

public class SearchHotAdapter extends RecyclerView.Adapter<SearchHotAdapter.SearchHotViewHolder> {
    private List<Hots> hotsList;
    private Context context;
    private BaseOnclickListener onclickListener;

    public SearchHotAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<Hots> searchHots){
        this.hotsList = searchHots;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public SearchHotViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SearchHotViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_hot, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull SearchHotViewHolder holder, int position) {
        Hots searchHot = hotsList.get(position);
        holder.tvIndex.setText(String.valueOf(position + 1));
        holder.tvSearch.setText(searchHot.getSearchWord());
        if (searchHot.getIconUrl() != null){
            Glide.with(context)
                    .load(searchHot.getIconUrl())
                    .into(holder.img);
        }

        holder.viewLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onclickListener != null){
                    onclickListener.click(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return hotsList == null ? 0 : hotsList.size();
    }

    class  SearchHotViewHolder extends RecyclerView.ViewHolder{
        TextView tvSearch;
        TextView tvIndex;
        ImageView img;
        View viewLayout;

        public SearchHotViewHolder(@NonNull View itemView) {
            super(itemView);

            tvSearch = itemView.findViewById(R.id.tv_search_hot);
            tvIndex = itemView.findViewById(R.id.tv_index);
            img = itemView.findViewById(R.id.img);
            viewLayout = itemView.findViewById(R.id.view_layout);
        }
    }

    public void setBaseOnclickListener(BaseOnclickListener onclickListener){
        this.onclickListener = onclickListener;
    }
}
